package com.gery.tn.ui;

import com.gery.tn.ui.elements.misc.AboutPanel;
import com.gery.tn.ui.elements.misc.AppointmentPanel;
import com.gery.tn.ui.elements.misc.ListContainer;
import com.gery.tn.ui.elements.misc.ClientPanel;
import com.gery.tn.ui.elements.misc.SettingsPanel;
import com.gery.tn.ui.elements.misc.StatusBar;
import com.gery.tn.ui.elements.misc.UploadPanel;
import com.gery.tn.ui.elements.search.ResultList;
import com.gery.tn.ui.elements.search.SearchPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;





public final class MainWindow extends JFrame {    
    
    private static final long serialVersionUID = 1L;
    
    private JPanel centerPanel;
    private JPanel contentPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel topPanel;
    
    private JButton closeButton;
    private JButton dragButton;
    private JButton homeButton;
    private JButton listButton;
    private JButton minimizeButton;
    private JButton newButton;
    private JButton settingsButton;
    private JButton uploadButton;
    
    private AboutPanel aboutPanel;
    private AppointmentPanel appointmentPanel;
    private ClientPanel clientPanel;
    private ListContainer listContainer;
    private ResultList resultList;
    private SearchPanel searchPanel;
    private SettingsPanel settingsPanel;
    private StatusBar statusBar;
    private UploadPanel uploadPanel;
    
    private JLabel searchLabel;
    private JLabel sep1;
    private JLabel sep2;
    private JLabel sep3;
    
    private ImageIcon buttonSeparator;
    private ImageIcon closeButtonNormal;
    private ImageIcon closeButtonHover;
    private ImageIcon dragButtonNormal;
    private ImageIcon homeButtonNormal;
    private ImageIcon listButtonDown;
    private ImageIcon listButtonHover;
    private ImageIcon listButtonNormal;
    private ImageIcon minimizeButtonNormal;
    private ImageIcon minimizeButtonHover;
    private ImageIcon newButtonDown;
    private ImageIcon newButtonHover;
    private ImageIcon newButtonNormal;
    private ImageIcon searchImage;
    private ImageIcon settingsButtonDown;
    private ImageIcon settingsButtonHover;
    private ImageIcon settingsButtonNormal;
    private ImageIcon uploadButtonDown;
    private ImageIcon uploadButtonHover;
    private ImageIcon uploadButtonNormal;
    
    private String dir = "com/gery/tn/ui/images/";
    private Dimension screenRes = Toolkit.getDefaultToolkit().getScreenSize();
    private Point lastPos = new Point(0, 0);
    private Point last2Pos = new Point(0, 0);
    private Point curPos = new Point(0, 0);

    
    
    public MainWindow() {
        setToCenter();
        
        
        aboutPanel = new AboutPanel();
        appointmentPanel = new AppointmentPanel(this);
        centerPanel = new JPanel();
        clientPanel = new ClientPanel(this);
        contentPanel = new JPanel();
        leftPanel = new JPanel();
        listContainer = new ListContainer(this);
        resultList = new ResultList(this);
        rightPanel = new JPanel();
        settingsPanel = new SettingsPanel();
        statusBar = new StatusBar(this);
        topPanel = new JPanel();
        uploadPanel = new UploadPanel(this);
        
        
        
        //--- Home Button
        homeButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "homeButtonNormal.png"));
        homeButton = new JButton(homeButtonNormal);
        homeButton.setBorder(null);
        homeButton.setPreferredSize(new Dimension(127, 35));
        //--- Drag Button
        dragButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "dragButton.png"));
        dragButton = new JButton(dragButtonNormal);
        dragButton.setBorder(null);
        dragButton.setPreferredSize(new Dimension(114, 35));
        //--- The Search
        searchImage = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "searchImage.png"));
        searchLabel = new JLabel(searchImage);
        searchPanel = new SearchPanel(this);
        //--- Minimize Button
        minimizeButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "minimizeButtonNormal.png"));
        minimizeButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "minimizeButtonHover.png"));
        minimizeButton = new JButton(minimizeButtonNormal);
        minimizeButton.setRolloverIcon(minimizeButtonHover);
        minimizeButton.setBorder(null);
        minimizeButton.setPreferredSize(new Dimension(39, 35));
        //--- Close Button
        closeButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "closeButtonNormal.png"));
        closeButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "closeButtonHover.png"));
        closeButton = new JButton(closeButtonNormal);
        closeButton.setRolloverIcon(closeButtonHover);
        closeButton.setBorder(null);
        closeButton.setPreferredSize(new Dimension(40, 35));
        //--- List Button
        listButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "listButtonNormal.png"));
        listButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "listButtonHover.png"));
        listButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "listButtonDown.png"));
        listButton = new JButton(listButtonNormal);
        listButton.setRolloverIcon(listButtonHover);
        listButton.setPressedIcon(listButtonDown);
        listButton.setBorder(null);
        listButton.setBounds(0, 5, 121, 120);
        //--- New Button
        newButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "newButtonNormal.png"));
        newButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "newButtonHover.png"));
        newButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "newButtonDown.png"));
        newButton = new JButton(newButtonNormal);
        newButton.setRolloverIcon(newButtonHover);
        newButton.setPressedIcon(newButtonDown);
        newButton.setBorder(null);
        newButton.setBounds(121, 5, 120, 120);
        //--- Settings Button
        settingsButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "settingsButtonNormal.png"));
        settingsButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "settingsButtonHover.png"));
        settingsButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "settingsButtonDown.png"));
        settingsButton = new JButton(settingsButtonNormal);
        settingsButton.setRolloverIcon(settingsButtonHover);
        settingsButton.setPressedIcon(settingsButtonDown);
        settingsButton.setBorder(null);
        settingsButton.setBounds(0, 130, 121, 120);
        //--- Upload Button
        uploadButtonNormal = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "uploadButtonNormal.png"));
        uploadButtonHover = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "uploadButtonHover.png"));
        uploadButtonDown = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "uploadButtonDown.png"));
        uploadButton = new JButton(uploadButtonNormal);
        uploadButton.setRolloverIcon(uploadButtonHover);
        uploadButton.setPressedIcon(uploadButtonDown);
        uploadButton.setBorder(null);
        uploadButton.setBounds(121, 130, 120, 120);
        //--- Separators
        buttonSeparator = new ImageIcon(this.getClass().getClassLoader().getResource(dir + "buttonSeparator.png"));
        sep1 = new JLabel(buttonSeparator);
        sep1.setBounds(0, 0, 241, 5);
        sep2 = new JLabel(buttonSeparator);
        sep2.setBounds(0, 125, 241, 5);
        sep3 = new JLabel(buttonSeparator);
        sep3.setBounds(0, 250, 241, 5);
        
        
        
        
        //Top Panel
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ((FlowLayout)topPanel.getLayout()).setHgap(0);
        topPanel.setPreferredSize(new Dimension(920, 40));
        topPanel.setBackground(new Color(0, 0, 0, 0));
        topPanel.add(homeButton);
        topPanel.add(dragButton);
        topPanel.add(searchLabel);
        topPanel.add(searchPanel);
        topPanel.add(minimizeButton);
        topPanel.add(closeButton);

        
        
        //Left Panel
        leftPanel.setPreferredSize(new Dimension(241, 530));
        leftPanel.setBackground(Color.LIGHT_GRAY);
        leftPanel.setLayout(null);
        leftPanel.setBorder(null);
        leftPanel.add(sep1);
        leftPanel.add(listButton);
        leftPanel.add(newButton);
        leftPanel.add(sep2);
        leftPanel.add(settingsButton);
        leftPanel.add(uploadButton);
        leftPanel.add(sep3);
        leftPanel.add(appointmentPanel);
        
        
        
        //Right Panel
        rightPanel.setLayout(null);
        rightPanel.setBorder(null);
        rightPanel.setPreferredSize(new Dimension(679, 530));
        rightPanel.add(resultList);
        rightPanel.add(listContainer);
        rightPanel.add(clientPanel);
        rightPanel.add(settingsPanel);
        rightPanel.add(uploadPanel);
        rightPanel.add(aboutPanel);
        rightPanel.add(statusBar);


        //Center Panel
        centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        ((FlowLayout)centerPanel.getLayout()).setHgap(0);
        ((FlowLayout)centerPanel.getLayout()).setVgap(0);
        centerPanel.setBackground(Color.RED);
        centerPanel.add(leftPanel);
        centerPanel.add(rightPanel);

        
        //Content Panel
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBorder(null);
        contentPanel.add(topPanel, BorderLayout.NORTH);
        contentPanel.add(centerPanel, BorderLayout.CENTER);
        this.setContentPane(contentPanel);


        
        
        
        
        //--- Event handling
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //fadeOut();
                System.exit(0);
            }
        });
        minimizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientPanel.setVisible(false);
                //minimizeWindow();
                MainWindow.this.setState(JFrame.ICONIFIED);
            }
        });
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientPanel.setVisible(false);
                clientPanel.disableAll();
                aboutPanel.setVisible(false);
                settingsPanel.setVisible(false);
                uploadPanel.setVisible(false);
                listContainer.initAppointments();
            }
        });
        homeButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                homeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                homeButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        });
        dragButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                curPos.x = e.getXOnScreen();
                curPos.y = e.getYOnScreen();
                last2Pos = MainWindow.this.getLocation();
            }
        });
        dragButton.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getXOnScreen();
                int y = e.getYOnScreen();
                MainWindow.this.setLocation(last2Pos.x + x - curPos.x, last2Pos.y + y - curPos.y);
            }
        });
        listButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientPanel.setVisible(false);
                clientPanel.disableAll();
                aboutPanel.setVisible(false);
                settingsPanel.setVisible(false);
                uploadPanel.setVisible(false);
                listContainer.initClients();
            }
        });
        newButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listContainer.setVisible(false);
                aboutPanel.setVisible(false);
                settingsPanel.setVisible(false);
                uploadPanel.setVisible(false);
                clientPanel.setID(0);
                clientPanel.openForNewClientInput();
                statusBar.setStatus("", 0);
            }
        });
        settingsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listContainer.setVisible(false);
                clientPanel.setVisible(false);
                clientPanel.disableAll();
                aboutPanel.setVisible(false);
                uploadPanel.setVisible(false);
                settingsPanel.setVisible(true);
            }
        });
        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listContainer.setVisible(false);
                clientPanel.setVisible(false);
                clientPanel.disableAll();
                aboutPanel.setVisible(false);
                settingsPanel.setVisible(false);
                uploadPanel.setVisible(true);
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent e) {
                MainWindow.this.setState(JFrame.NORMAL);
                 //maximizeWindow();
            }
        });
        
    }

    
    
    //--- Getters
    public ResultList getResultList() {
        return resultList;
    }
    public AppointmentPanel getAppointmentPanel() {
        return appointmentPanel;
    }
    public SearchPanel getSearchPanel() {
        return searchPanel;
    }
    public ClientPanel getClientPanel() {
        return clientPanel;
    }
    public ListContainer getListContainer() {
        return listContainer;
    }
    public StatusBar getStatusBar() {
        return statusBar;
    }
    public AboutPanel getAboutPanel() {
        return aboutPanel;
    }
    public SettingsPanel getSettingsPanel() {
        return settingsPanel;
    }
    public UploadPanel getUploadPanel() {
        return uploadPanel;
    }
    
    
    
    //--- Other methods
    public void minimizeWindow() {
        lastPos.setLocation(this.getX(), this.getY());
        int left = this.getX();
        int top = this.getY();
        int target = (int)screenRes.getHeight();
        for (int i=top; i<target; i++){
                this.setLocation(left, i);
        }
    }
    public void maximizeWindow() {
            int top = (int)screenRes.getHeight();
            int left = (int)lastPos.getX();
            for (int i=top; i>lastPos.getY(); i--) {
                    this.setLocation(left, i);
            }
    }
    public void fadeOut() {
        float op = 0.99f;
        while (op > 0) {
            this.setOpacity(op);
            op -= 0.02f;
        }
    }
    public void setToCenter() {
        this.setUndecorated(true);
        int left = (int)((screenRes.getWidth() - 970) / 2);
        int top = (int)((screenRes.getHeight() - 570) / 2);
        this.setBounds(left, top, 920, 570);
        lastPos.setLocation(left, top);
    }
    
}
